package Operation;

import controller.DataAccessHandler;
import model.pojo.Account;
import util.MenuInputUtil;

import java.util.Scanner;

/**
 * 账户信息修改
 */
public class ModifyAccountIOperation implements IOperation {
    @Override
    public void operate() {
        System.out.println("<修改指定账户信息>");
        //输入要修改的账户的ID
        Scanner input = new Scanner(System.in);
        System.out.print("输入账户ID: ");
        String accountId = input.next();
        //查询该账号是否存在
        String sql = "select account_id as accountId,keyword,account_number as accountNumber," +
                "password,remark,creation_time as creationTime,last_modification_time as lastModificationTime " +
                "from tb_accounts " +
                "where account_id = ?";
        Account account = DataAccessHandler.queryOne(Account.class, sql, accountId);
        if(account == null){
            System.out.println("Account doesn't exist!");
            return;
        }
        //打印原账号信息
        account.printAccountInfoDetail();
        //输入要修改的信息
        System.out.print("Keyword：");
        String keyword = MenuInputUtil.readString(64, account.getKeyword());
        System.out.print("AccountNumber：");
        String accountNumber = MenuInputUtil.readString(64, account.getAccountNumber());
        System.out.print("Password：");
        String password = MenuInputUtil.readString(64, account.getPassword());
        System.out.print("Remark：");
        String remark = MenuInputUtil.readString(64, account.getRemark());
        //修改数据库中对应的账户信息
        sql = "update tb_accounts set keyword = ?,account_number = ?,password = ?,remark = ?," +
                "last_modification_time = now() " +
                "where account_id = ?";
        boolean isSuccess = DataAccessHandler.update(sql, keyword, accountNumber, password, remark, accountId);
        if(isSuccess){
            System.out.println("Modified successfully!");
        }else{
            System.out.println("Failed modification!");
        }
    }
}
